连接服务器

使用 telnet 客户端工具可以连接到 memcached 服务器。

1
telnet 192.168.1.102 11211

第一次进来需要摸黑输入,可以敲个回车,同时忽略 ERROR 消息。

set 语法

1
2
3
set key flags exptime bytes
value
参数 描述
key 键的名称
flags 32位无符号整数,用于存储键值对的额外信息
exptime 缓存过期时间(单位秒),0 表示永不过期
bytes 缓存的 value 值的字节数,不是键的字节数
value 缓存的 value 值,在新的一行赋予

作用:若 key 不存在,则存储该键值对;若 key 已经存在,则更新该 key 的 value 值。

示例:

1
2
3
4
5
set name 0 0 9
fanlychie
STORED

STORED 表示成功,ERROR 表示错误。

get 语法

1
get key

作用:获取 key 映射的 value 值。

示例:

1
2
3
4
5
6
7
get name
VALUE name 0 9
fanlychie
END

“VALUE name 0 9” 0 是 flags 的值,9 是 value 的字节数。

add 语法

1
2
3
add key flags exptime bytes
value

参数含义参考 set 语法参数。

作用:添加一个新的键,如果该键已经存在,则返回 NOT_STORED。

示例:

1
2
3
4
5
add sex 0 0 4
male
STORED

append 语法

1
2
3
append key flags exptime bytes
value

参数含义参考 set 语法参数。

作用:追加新的值到一个现有的键映射的值中,如果键不存在,则返回 NOT_STORED。

示例:

1
2
3
4
5
6
7
get sex
VALUE sex 0 4
male
END
1
2
3
4
5
append sex 0 0 3
abc
STORED
1
2
3
4
5
6
7
get sex
VALUE sex 0 4
maleabc
END

replace 语法

1
2
3
replace key flags exptime bytes
value

参数含义参考 set 语法参数。

作用:替换一个现有的键映射的值,如果键不存在,则返回 NOT_STORED。

示例:

1
2
3
4
5
6
7
get sex
VALUE sex 0 4
maleabc
END
1
2
3
4
5
replace sex 0 0 4
male
STORED
1
2
3
4
5
6
7
get sex
VALUE sex 0 4
male
END

delete 语法

1
delete key

作用:删除缓存。若 key 不存在,则返回 NOT_FOUND;成功则返回 DELETE。

示例:

1
2
3
delete sex
DELETE

incr 语法

1
incr key increment_value

作用:使键映射的值增长。

示例:

1
2
3
4
5
set age 0 0 2
21
STORED
1
2
3
incr age 1
22

decr 语法

1
decr key increment_value

作用:使键映射的值减小。

示例:

1
2
3
4
5
set age 0 0 2
21
STORED
1
2
3
decr age 1
20

stats 语法

1
stats

作用:统计服务器信息。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
stats
STAT pid 32586
STAT uptime 1887
STAT time 1445786677
STAT version 1.4.24
STAT libevent 2.0.22-stable
STAT pointer_size 64
STAT rusage_user 0.085986
STAT rusage_system 0.249962
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 12
STAT cmd_set 20
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 7
STAT get_misses 5
STAT delete_misses 0
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 1
STAT decr_misses 0
STAT decr_hits 1
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 986
STAT bytes_written 4495
STAT limit_maxbytes 134217728
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 220
STAT curr_items 3
STAT total_items 6
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
END
消息 描述
pid Memcache 进程 ID
uptime 服务器已运行秒数
time 服务器当前 Unix 时间戳
version Memcache 版本
pointer_size 操作系统指针大小
curr_connections 当前连接数量
total_connections Memcached 运行以来连接总数
connection_structures Memcached 分配的连接结构数量
cmd_get get 命令请求次数
cmd_set set 命令请求次数
cmd_flush flush 命令请求次数
get_hits get 命令命中次数
get_misses get 命令未命中次数
delete_misses delete 命令未命中次数
delete_hits delete 命令命中次数
incr_misses incr 命令未命中次数
incr_hits incr 命令命中次数
decr_misses decr 命令未命中次数
decr_hits decr 命令命中次数
cas_misses cas 命令未命中次数
cas_hits cas 命令命中次数
cas_badval 使用擦拭次数
auth_cmds 认证命令处理的次数
auth_errors 认证失败数目
bytes_read 读取总字节数
bytes_written 发送总字节数
limit_maxbytes 分配的内存总大小(字节)
accepting_conns 接受新的连接
listen_disabled_num 失效的监听数
threads 当前线程数
conn_yields 连接操作主动放弃数目
bytes 当前存储占用的字节数
curr_items 当前存储的数据总数
total_items 启动以来存储的数据总数
evictions LRU 释放的对象数目

unix 时间戳转换(java)

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
Date date = new Date(1445786677L * 1000);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = format.format(date);
System.out.println(dateStr);
}

stats items 语法

1
stats items

作用:统计分配的项信息。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stats items
STAT items:1:number 3
STAT items:1:age 1794
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
STAT items:1:crawler_reclaimed 0
STAT items:1:crawler_items_checked 0
STAT items:1:lrutail_reflocked 0
END

“STAT items:1:number 3”,1 是 slab_id,3 表示总共有 3 条缓存数据。

stats cachedump 语法

1
stats cachedump slab_id limit_num
参数 描述
slab_id slab 编号
limit_num 返回的记录条数,0 表示不限制,即所有

作用:返回指定编号的 slab 中键的信息列表。

示例:

1
2
3
4
5
6
7
8
9
stats cachedump 1 0
ITEM sex [4 b; 1445784730 s]
ITEM age [2 b; 1445784730 s]
ITEM name [9 b; 1445784730 s]
END

“ITEM sex [4 b; 1445784730 s]”,sex 为键的名称,4 为键映射的 value 值的字节数,1445784730 为过期时间。

flush_all 语法

1
flush_all

作用:清除所有缓存数据。

示例:

1
2
3
flush_all
OK